package com.gongpingjialoan.view;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.category.CategoryActivity;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.bean.car.SortBean;
import com.gongpingjialoan.constant.Const;
import com.gongpingjialoan.utility.StepMonitor;
import com.gongpingjialoan.utility.Utils;
import com.gongpingjialoan.widget.PricePopupWindow;
import com.gongpingjialoan.widget.SortPopopWindow;

/**
 * Created by Administrator on 2016/5/17.
 */
public class NewSortLinearLayout extends LinearLayout implements View.OnClickListener {

    private static final int REQUEST_BRAND_SORT = 0x298;

    private TextView indexTextView, brandTextView, priceTextView, sortTextView;

    private View brandView, priceView, indexView, sortView;

    private GeCarList mGeCarList;

    private String index = "";

    private Fragment carListFragment;

    private SortPopopWindow mSortPopopWindow;

    private PricePopupWindow mPricePopupWindow;

    private String maxPrice = "1000";

    private String sort = "", price = "", brand = "", brandName = "", model = "", modelName = "", cut_price = "0", is_certify = "0";


    public NewSortLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(getContext()).inflate(R.layout.new_sort_layout, this, true);
        brandView = findViewById(R.id.brand);
        priceView = findViewById(R.id.price);
        indexView = findViewById(R.id.index);
        sortView = findViewById(R.id.sort);
        indexTextView = (TextView) findViewById(R.id.sort_index);
        brandTextView = (TextView) findViewById(R.id.sort_brand);
        priceTextView = (TextView) findViewById(R.id.sort_price);
        sortTextView = (TextView) findViewById(R.id.sort_id);


        brandView.setOnClickListener(this);
        priceView.setOnClickListener(this);
        indexView.setOnClickListener(this);
        sortView.setOnClickListener(this);

        mPricePopupWindow = new PricePopupWindow(getContext());
        mPricePopupWindow.setISelctedPrice(new PricePopupWindow.ISelctedPrice() {
            @Override
            public void select(String p) {
                if (TextUtils.isEmpty(p)) {
                    Utils.setTextViewImage(getContext(), priceTextView, R.drawable.sort_normal_new, R.color.text_details_color);
                } else {
                    Utils.setTextViewImage(getContext(), priceTextView, R.drawable.sort_focus_new, R.color.new_color);
                }
                price = p;
                mGeCarList.getCarList(sort, price, brand, brandName, model, modelName, cut_price, is_certify);
            }
        });

        mSortPopopWindow = new SortPopopWindow(getContext());
        mSortPopopWindow.setISelcted(new SortPopopWindow.ISelcted() {
            @Override
            public void select(SortBean bean, boolean isCutPrice, boolean isGoodCar) {
                if (bean != null) {
                    sort = bean.getValue();
                }
                if (isCutPrice) {
                    cut_price = "1";
                } else {
                    cut_price = "0";
                }
                if (isGoodCar) {
                    is_certify = "1";
                } else {
                    is_certify = "0";
                }

                if ("1".equals(cut_price) || "1".equals(is_certify) || (!TextUtils.isEmpty(sort) && !"-gpj_index".equals(sort))) {
                    Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_focus_new, R.color.new_color);
                } else {
                    Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_normal_new, R.color.text_details_color);
                }

                if (!"-gpj_index".equals(sort)) {
                    index = "";
                    indexTextView.setTextColor(getResources().getColor(R.color.text_details_color));
                }
                mGeCarList.getCarList(sort, price, brand, brandName, model, modelName, cut_price, is_certify);
            }
        });

    }


    public void setNoCUT() {
        if (mSortPopopWindow != null) {
            mSortPopopWindow.setCut(false);
            cut_price = "0";
            if ("1".equals(cut_price) || "1".equals(is_certify) || !"".equals(sort)) {
                if (sort.contains("gpj_index")) {
                    indexTextView.setTextColor(getResources().getColor(R.color.new_color));
                    Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_normal_new, R.color.text_details_color);
                } else {
                    Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_focus_new, R.color.new_color);
                    index = "";
                    indexTextView.setTextColor(getResources().getColor(R.color.text_details_color));
                }
            } else {
                Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_normal_new, R.color.text_details_color);
            }
        }
    }


    public void setMaxPrice(String maxPrice) {
        this.maxPrice = maxPrice;
        if (!TextUtils.isEmpty(maxPrice)) {
            if (mPricePopupWindow != null) {
                int p = (int) Double.parseDouble(maxPrice) + 1;
                mPricePopupWindow.setMax(p);
            }
        } else {
            if (mPricePopupWindow != null) {
                mPricePopupWindow.setMax(1000);
            }
        }
    }


    public void setNoGoodCar() {
        if (mSortPopopWindow != null) {
            mSortPopopWindow.setGoodCar(false);
            is_certify = "0";
            if ("1".equals(cut_price) || "1".equals(is_certify) || !"".equals(sort)) {
                if (sort.contains("gpj_index")) {
                    indexTextView.setTextColor(getResources().getColor(R.color.new_color));
                    Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_normal_new, R.color.text_details_color);
                } else {
                    Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_focus_new, R.color.new_color);
                    index = "";
                    indexTextView.setTextColor(getResources().getColor(R.color.text_details_color));
                }
            } else {
                Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_normal_new, R.color.text_details_color);
            }
        }
    }


    public void setNoSortColor(String sort, String is_certify, String cut_price) {
        this.cut_price = cut_price;
        this.sort = sort;
        this.is_certify = is_certify;
        if ("1".equals(cut_price) || "1".equals(is_certify) || !"".equals(sort)) {
            if (sort.contains("gpj_index")) {
                indexTextView.setTextColor(getResources().getColor(R.color.new_color));
                Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_normal_new, R.color.text_details_color);
            } else {
                Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_focus_new, R.color.new_color);
                index = "";
                indexTextView.setTextColor(getResources().getColor(R.color.text_details_color));
                if ("1".equals(is_certify)) {
                    if (mSortPopopWindow != null) {
                        mSortPopopWindow.setGoodCar(true);
                    }
                } else {
                    if (mSortPopopWindow != null) {
                        mSortPopopWindow.setGoodCar(false);
                    }
                }
                if ("1".equals(cut_price)) {
                    if (mSortPopopWindow != null) {
                        mSortPopopWindow.setCut(true);
                    }
                } else {
                    if (mSortPopopWindow != null) {
                        mSortPopopWindow.setCut(false);
                    }
                }
                if("distance".equals(sort)){
                    setPosition(3);
                } else {
                    setPosition(-1);
                }
            }
        } else {
            Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_normal_new, R.color.text_details_color);
            indexTextView.setTextColor(getResources().getColor(R.color.text_details_color));
        }
    }


    public void setPosition(int pos) {
        if (mSortPopopWindow != null) {
            mSortPopopWindow.setPosition(pos);
        }

    }


    @Override
    public void onClick(View v) {
        View parent = findViewById(R.id.line);
        if (v == indexView) {
            StepMonitor.getInstance().addMonitor(new Monitor("click", "buy_market_header_gpjIndexRank", System.currentTimeMillis(), 1f));
            if ("".equals(index)) {
                indexTextView.setTextColor(getResources().getColor(R.color.new_color));
                index = "-gpj_index";
                if ("0".equals(is_certify) && "0".equals(cut_price) && !TextUtils.isEmpty(sort)) {
                    Utils.setTextViewImage(getContext(), sortTextView, R.drawable.sort_normal_new, R.color.text_details_color);
                }
                setPosition(-1);
                mSortPopopWindow.sortBean = null;
            } else {
                indexTextView.setTextColor(getResources().getColor(R.color.text_details_color));
                index = "";
            }
            sort = index;
            mGeCarList.getCarList(sort, price, brand, brandName, model, modelName, cut_price, is_certify);
        } else if (v == brandView) {
            StepMonitor.getInstance().addMonitor(new Monitor("click", "buy_market_header_brandFilter", System.currentTimeMillis(), 1f));
            Intent intent = new Intent();
            intent.setClass(getContext(), CategoryActivity.class);
            intent.putExtra("needModelDetailFragment", false);
            intent.putExtra("isFromFilter", true);
            intent.putExtra(Const.BRAND_NAME_KEY, brandName);
            intent.putExtra(Const.BRAND_SLUG_KEY, brand);
            intent.putExtra(Const.MODEL_NAME_KEY, modelName);
            intent.putExtra(Const.MODEL_SLUG_KEY, model);
//            intent.putExtra(Const.MODEL_DETAIL_NAME_KEY, "");
//            intent.putExtra(Const.MODEL_DETAIL_SLUG_KEY, "");
            carListFragment.startActivityForResult(intent, REQUEST_BRAND_SORT);

        } else if (v == priceView) {
            StepMonitor.getInstance().addMonitor(new Monitor("click", "buy_market_header_priceFilter", System.currentTimeMillis(), 1f));
            Utils.showAsDropDown(parent,mPricePopupWindow);

        } else if (v == sortView) {
            StepMonitor.getInstance().addMonitor(new Monitor("click", "buy_market_header_rank", System.currentTimeMillis(), 1f));
            Utils.showAsDropDown(parent,mSortPopopWindow);
        }

    }

    public String getIs_certify() {
        return is_certify;
    }


    public void setBrandColor() {
        if (!"".equals(brand)) {
            Utils.setTextViewImage(getContext(), brandTextView, R.drawable.sort_focus_new, R.color.new_color);
        } else {
            Utils.setTextViewImage(getContext(), brandTextView, R.drawable.sort_normal_new, R.color.text_details_color);
        }
    }


    public void setPriceView(String mPrice) {
        if (!"".equals(mPrice)) {
            Utils.setTextViewImage(getContext(), priceTextView, R.drawable.sort_focus_new, R.color.new_color);
            price = mPrice;
        } else {
            Utils.setTextViewImage(getContext(), priceTextView, R.drawable.sort_normal_new, R.color.text_details_color);
            price = "";
        }

    }


    public void setGeCarList(GeCarList geCarList) {
        mGeCarList = geCarList;
        carListFragment = (Fragment) geCarList;
    }


    public void clearPrice() {
        price = "";
        Utils.setTextViewImage(getContext(), priceTextView, R.drawable.sort_normal_new, R.color.text_details_color);
        mPricePopupWindow.clearPrice();
    }


    public interface GeCarList {
        public void getCarList(String sort, String price, String brand, String brandName, String model, String modelName, String cut_price, String is_certify);
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getModelName() {
        return modelName;
    }

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }

    public String getCut_price() {
        return cut_price;
    }

    public void setCut_price(String cut_price) {
        this.cut_price = cut_price;
    }
}
